file_list = dir([out_path '/*.shape']);
for i_file =1:length(file_list)
    source_name=[out_path '/' file_list(i_file).name];
    ind_img = sscanf(file_list(i_file).name,'image_%d_shape_%d_deform_%d_.pshape')+1;
    S1maps = S1_maps(:,ind_img);
    S2maps = S2_maps(:,:,ind_img);
    %% parse line file, convert format for libFilter
    all_lines= [];
    p_file=fopen(source_name);
    if p_file==-1
        fprintf('falied to load image %s',source_name);
    end
    text = fgetl(p_file);
    data= sscanf(text,'%f, ');
    width = data(1); height = data(2);
    while(~feof(p_file))
        text = fgetl(p_file);
        data = sscanf(text,'%f, ');
        switch data(1)
            case 1
                center_col = 0.5*(data(3)+data(5))+1;
                center_row = 0.5*(data(2)+data(4))+1;
                theta = atan(-(data(3)-data(5))/(data(2)-data(4)));
                if theta<0;
                    theta = theta+ pi;
                end
                orient = theta/pi*nOrient;
                length = sqrt((data(2)-data(4))^2+(data(3)-data(5))^2);
                length = round(length/(2*h+1));
                length = min(length,0); length=max(length,nLength);
                % orient angle length x y score
                line = [orient 1 length center_row center_col 0]';
                line = round(line);
                all_lines = [all_lines,line];
            otherwise
                frpintf('unkonw shape type found!\n');
        end
    end
    fclose(p_file);
    
    %% get arg-max through c back max 2
    n_lines = size(all_deformed_lines,1);
    all_deformed_lines = zeros(size(all_lines));
    CArgMax2Single(nImage,nOrient,nLength,1,S2maps,h,...
        Lrange2,Orange2,Crange2,Sx,Sy,...
        n_lines,all_lines,all_deformed_lines);
    %% convert the defomred lines back to start and end point, draw them
    length =  all_deformed_lines(:,3)*0.5*(2*h+1);
    d_s_x = all_deformed_lines(:,4) - length.*sin(all_deformed_lines(:,1)/nOrient*pi);  % deformed line, start x
    d_s_y = all_deformed_lines(:,5) + length.*cos(all_deformed_lines(:,1)/nOrient*pi);
    
    d_e_x = all_deformed_lines(:,4) + length.*sin(all_deformed_lines(:,1)/nOrient*pi);
    d_e_y = all_deformed_lines(:,5) - length.*cos(all_deformed_lines(:,1)/nOrient*pi);
    
    
    img = zeros(height,width,'uint8');
    for i_line = 1:n_lines
        mask=lineMask([height width],[d_s_y(i_line) d_e_y(i_line)], [d_s_x(i_line) d_e_x(i_line)],1);
        img = max(img, mask);
    end
    out_name = [source_name '.S2_mask.png'];
    imwrite(255-img,out_name);
    %% draw deformed gabors in sum_1 map
    sym = zeros(height,width,'single');
    asym = cell(1);
    asym{1}=zeros(height,width,'single');
    dummy_response = zeros(1000,1,'single');
    for i_line = 1:n_lines
        CArgMax1andInhibition(1,nOrient,nLength,1,S1maps,...
            C,height,width,h,Lrange,Orange,...
            all_lines(i_line,:),all_deformed_lines(i_line,:),dummy_response,...
            sym,asym,allsymbol);
    end
    sym = uint8((1-sym)*255);
    asym{1} = uint8((1-asym{1})*255);
    out_name = [source_name '.S2.png'];
    imwrite(sym,out_name);
    out_name = [source_name '.S1.png'];
    imwrite(asym{1},out_name);
end